/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.appletShow.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.appletShow.domain.CmsService;
import com.fzhucc.modules.appletShow.domain.CmsServiceMsgBox;
import com.fzhucc.modules.appletShow.domain.dto.CmsServiceBoxSeekDto;
import com.fzhucc.modules.appletShow.domain.dto.CmsServiceMsgBoxDto;
import com.fzhucc.modules.appletShow.domain.dto.CmsServiceMsgBoxQueryCriteria;
import com.fzhucc.modules.appletShow.repository.CmsServiceMsgBoxRepository;
import com.fzhucc.modules.appletShow.repository.CmsServiceRepository;
import com.fzhucc.modules.appletShow.service.mapstruct.CmsServiceMsgBoxMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-03-14
 **/
@Service
@RequiredArgsConstructor
public class CmsServiceMsgBoxService {

    private final CmsServiceMsgBoxRepository cmsServiceMsgBoxRepository;

    private final CmsServiceRepository cmsServiceRepository;
    private final CmsServiceMsgBoxMapper cmsServiceMsgBoxMapper;

    public Map<String, Object> queryAll(CmsServiceMsgBoxQueryCriteria criteria, Pageable pageable) {
        Page<CmsServiceMsgBox> page = cmsServiceMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        Page<CmsServiceMsgBoxDto> map = page.map(cmsServiceMsgBoxMapper::toDto);
        List<CmsServiceMsgBoxDto> list = map.getContent();
        if (list != null) {
            for (CmsServiceMsgBoxDto cmsServiceMsgBoxDto : list) {
                Optional<CmsService> res = cmsServiceRepository.findById(cmsServiceMsgBoxDto.getServiceId());
                if (res.isPresent()) {
                    cmsServiceMsgBoxDto.setService(res.get());
                }
            }
        }
        return PageUtil.toPage(map);
    }

    public List<CmsServiceMsgBoxDto> queryAll(CmsServiceMsgBoxQueryCriteria criteria) {
        return cmsServiceMsgBoxMapper.toDto(cmsServiceMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    public CmsServiceMsgBoxDto findById(Integer id) {
        CmsServiceMsgBox cmsServiceMsgBox = cmsServiceMsgBoxRepository.findById(id).orElseGet(CmsServiceMsgBox::new);
        ValidationUtil.isNull(cmsServiceMsgBox.getId(), "CmsServiceMsgBox", "id", id);
        return cmsServiceMsgBoxMapper.toDto(cmsServiceMsgBox);
    }

    @Transactional(rollbackFor = Exception.class)
    public CmsServiceMsgBoxDto create(CmsServiceMsgBox resources) {
        return cmsServiceMsgBoxMapper.toDto(cmsServiceMsgBoxRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(CmsServiceMsgBox resources) {
        CmsServiceMsgBox cmsServiceMsgBox = cmsServiceMsgBoxRepository.findById(resources.getId()).orElseGet(CmsServiceMsgBox::new);
        ValidationUtil.isNull(cmsServiceMsgBox.getId(), "CmsServiceMsgBox", "id", resources.getId());
        cmsServiceMsgBox.copy(resources);
        cmsServiceMsgBoxRepository.save(cmsServiceMsgBox);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            cmsServiceMsgBoxRepository.deleteById(id);
        }
    }

    public void download(List<CmsServiceMsgBoxDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CmsServiceMsgBoxDto cmsServiceMsgBox : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("服务id", cmsServiceMsgBox.getServiceId());
            map.put("咨询内容", cmsServiceMsgBox.getContent());
            map.put("联系方式", cmsServiceMsgBox.getPhone());
            map.put("咨询时间", cmsServiceMsgBox.getConsultationTime());
            map.put("咨询人姓名", cmsServiceMsgBox.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public CmsServiceMsgBoxDto seek(CmsServiceBoxSeekDto resources) {
        CmsServiceMsgBox cmsServiceMsgBox = new CmsServiceMsgBox();
        Optional<CmsService> res = cmsServiceRepository.findById(resources.getServiceId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该服务");
        }
        cmsServiceMsgBox.setServiceId(resources.getServiceId());
        cmsServiceMsgBox.setPhone(resources.getPhone());
        cmsServiceMsgBox.setConsultationTime(new Timestamp(new Date().getTime()));
        cmsServiceMsgBox.setContent(resources.getContent());
        cmsServiceMsgBox.setName(resources.getName());
        CmsServiceMsgBox cmsServiceMsgBox1 = cmsServiceMsgBoxRepository.save(cmsServiceMsgBox);
        return cmsServiceMsgBoxMapper.toDto(cmsServiceMsgBox1);
    }
}